'use strict';

/**
 * @ngdoc service
 * @name jgappApp.usertaskService
 * @description
 * # usertaskService
 * Service in the jgappApp.
 */
angular.module('jgappApp')
    .service('UsertaskService', function($http, Util, $filter) {
        // AngularJS will instantiate a singleton by calling "new" on this function
        var UsertaskService = function(url, options) {
            this.items = [];
            this.busy = false;
            this.limit = 10;
            this.start = 0;
            this.pageNo = 0;
            this.noData = true;
            this.tip = "";
            this.url = url;
            this.options = options||{};
            this.nextPage();
            this.count = 0;
        };
        UsertaskService.prototype.nextPage = function() {
            if (!this.noData) {
                return;
            }
            if (this.busy) {
                return;
            }
            this.busy = true;
            this.start = (this.pageNo++) * this.limit;
            this.options.start = this.start
            this.options.limit = this.limit
            $http.get(this.url, {
                params: this.options
            }).
            success(function(data, status, headers, config) {
                if (data.logout) {
                    Util.logout();
                    return;
                }
                if (data.success) {
                    // 如果没有数据则返回
                    if (!data.dataList) {
                        this.noData = false;
                        this.tip = "没有数据";
                        return;
                    }
                    var items = data.dataList;
                    this.count = data.totalCount;
                    for (var i = 0; i < items.length; i++) {
                        // this.items.push(items[i]);
                        this.listFactory(items[i]);
                        // this.items.push(dataitem);
                    }
                    if (this.limit > items.length) {
                        this.noData = false;
                        if (this.items.length < 1) {
                            this.tip = "没有数据";
                        }
                        return
                    }
                } else {
                    this.noData = false;
                    this.tip = "没有数据";
                }
                this.busy = false;
            }.bind(this)).
            error(function(data, status, headers, config) {}.bind(this));
        };
        // 构造带有日期的数组函数
        UsertaskService.prototype.listFactory = function(data) {
            var dataitem;
            var exsit = false; //默认不存在
            var date = $filter("dateFmt")(data.createTime)
            var that = this;
            that.items.forEach(function(item, index) {
                if (item.date == date) {
                    that.items[index].dataList.push(data);
                    exsit = true;
                    return;
                }
            });
            if (!exsit) {
                this.items.push({
                    "date": date,
                    "dataList": [data]
                });
            }
            return this.items;

        };
        return UsertaskService;

    });
